package org.alex.sample.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.sample.model.ExamTest;
import org.alex.sample.model.ExamTestType;
import org.alex.sample.model.Subject;
import org.alex.sample.page.PagerModel;
import org.alex.sample.service.ExamTestService;
import org.alex.sample.service.ExamTestTypeService;
import org.alex.sample.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ExamTestController {
	
	@Autowired
	private ExamTestService examTestService;
	
	@Autowired
	private SubjectService subjectService;
	
	@Autowired
	private ExamTestTypeService examTestTypeService;

	//获取全部试题
	@RequestMapping(value = "tests")
	public String tests(Model model) throws Exception{
		PagerModel<ExamTest> examTestList = examTestService.findAllByPage();
		model.addAttribute("examTestList",examTestList);
		return "examTest/examTestList";
	}
	
	//获取科目名称
	@ResponseBody
	@RequestMapping(value = "/getSubjectName", method = RequestMethod.POST)
	public Map<Integer, String> getSubjectName() throws Exception {
		List<Subject> subjectList = subjectService.findAllSubject();
		Map<Integer, String> subjectName = new HashMap<Integer, String>();
		for(Subject subject:subjectList) {
			if(!subjectName.containsValue(subject.getName())) {
				subjectName.put(subject.getSubId(), subject.getName());
			}
		}
		return subjectName;
	}
	
	//获取试题类型
	@ResponseBody
	@RequestMapping(value = "/getExamTestType", method = RequestMethod.POST)
	public Map<Integer, String> getExamTestType() throws Exception{
		List<ExamTestType> examTestTypeList = examTestTypeService.findAllExamTestType();
		Map<Integer, String> examTestType = new HashMap<Integer, String>();
		for(ExamTestType testType:examTestTypeList) {
			if(!examTestType.containsValue(testType.getTestType())) {
				examTestType.put(testType.getId(), testType.getTestType());
			}
		}
		return examTestType;
	}
	
	//添加考题
	@ResponseBody
	@RequestMapping(value = "/addExamTest", method = RequestMethod.POST)
	public List<String> addExamTest(@Validated ExamTest examTest, BindingResult result) throws Exception{
		List<String> list = new ArrayList<String>();
		if(result.hasErrors()){
			list.add("f");
		}else{
			examTestService.createTest(examTest);
			list.add("t");
		}
		return list;
	}
	
	//修改考题
	@ResponseBody
	@RequestMapping(value = "/editExamTest", method = RequestMethod.POST)
	public List<String> editExamTest(@Validated ExamTest examTest, BindingResult result) throws Exception{
		List<String> list = new ArrayList<String>();
		if(result.hasErrors()){
			list.add("f");
		}else{
			examTestService.editTest(examTest);
			list.add("t");
		}
		return list;
	}
	
	//删除试题
	@RequestMapping(value = "deleteExamTest/{id}")
	public String deleteExamTest(RedirectAttributes redirectAttributes, @PathVariable Integer id) throws Exception {
		examTestService.deleteTest(id);
		redirectAttributes.addFlashAttribute("message", "已删除!");
		return "redirect:/tests";
	}
	
	//根据ID查找试题
	@ResponseBody
	@RequestMapping(value = "/findTestById", method = RequestMethod.POST)
	public Map<String, String> findTestById(Integer id) throws Exception{
		ExamTest examTest = examTestService.findTestById(id);
		Map<String, String> testList = new HashMap<String, String>();
		testList.put("examTestId", id + "");
		testList.put("subjectName", examTest.getSubject().getName());
		testList.put("content", examTest.getContent());
		testList.put("chooseA", examTest.getChooseA());
		testList.put("chooseB", examTest.getChooseB());
		testList.put("chooseC", examTest.getChooseC());
		testList.put("chooseD", examTest.getChooseD());
		testList.put("answer", examTest.getAnswer());
		testList.put("testTypeScore", examTest.getExamTestType().getTestTypeScore() + "");
		testList.put("testType", examTest.getExamTestType().getTestType());
		return testList;
	}
	
}
